package mo.tags.stack;

import java.util.Stack;

public class BrowserHistory {

    private Stack<String> cur = new Stack<>();
    private Stack<String> back = new Stack<>();

    public BrowserHistory(String homepage) {
        cur.push(homepage);
    }

    public void visit(String url) {
        cur.push(url);
        back.clear();
    }

    public String back(int steps) {
        for (int i = 0; i < steps; i++) {
            if (cur.size() == 1) {
                break;
            }
            String pop = cur.pop();
            back.push(pop);
        }
        return cur.peek();
    }

    public String forward(int steps) {
        for (int i = 0; i < steps; i++) {
            if (back.isEmpty()) {
                break;
            }
            cur.push(back.pop());
        }
        return cur.peek();
    }

}
